package demo;

import cn.laixueit.mapper.GameMapper;
import cn.laixueit.pojo.Game;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"})
public class Demo1 {

    @Autowired
    private GameMapper gameMapper;

    @Test
    public void getall(){
        List<Game> gameList = gameMapper.selectList(null);
        System.out.println(gameList);
    }

    @Test
    public void save(){
        Game game = new Game();
        game.setName("亮亮");
        game.setTitle("亮亮穿lolita");
        game.setCreatetime(new Date());
        int i = gameMapper.insert(game);
        System.out.println(i);
        System.out.println(game);
    }

    @Test
    public void del(){
        int i = gameMapper.deleteById(2);
        System.out.println(i);
    }

    @Test
    public void del2(){
        int i = gameMapper.deleteBatchIds(Arrays.asList(1, 3));
        System.out.println(i);
    }

    @Test
    public void del3(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","原神");
//        map.put()
        int i = gameMapper.deleteByMap(map);
        System.out.println(i);
    }

    @Test
    public void del4(){
        QueryWrapper<Game> wrapper = new QueryWrapper<>();
        wrapper.eq("id","5");
        int i = gameMapper.delete(wrapper);
        System.out.println(i);
    }

    @Test
    public void del5(){
        QueryWrapper<Game> wrapper = new QueryWrapper<>();
        wrapper.like("title","亮");
        int i = gameMapper.delete(wrapper);
        System.out.println(i);
    }


    @Test
    public void update(){
        Game game = new Game();
        game.setId(1);
        game.setName("张然");
        int i = gameMapper.updateById(game);
        System.out.println(i);
    }

    @Test
    public void update2(){
        UpdateWrapper<Game> wrapper = new UpdateWrapper<>();
        wrapper.like("title","女装");
        Game game = new Game();
        game.setId(1);
//        game.setName("张然");
        game.setTitle("然然穿jk");
        int i = gameMapper.update(game,wrapper);
        System.out.println(i);
    }

    @Test
    public void update3(){
        UpdateWrapper<Game> wrapper = new UpdateWrapper<Game>();
        wrapper.gt("id","0");
        Game game = new Game();
        game.setId(1);
//        game.setName("张然");
        game.setTitle("落地成盒");
        int i = gameMapper.update(game,wrapper);
        System.out.println(i);
    }

    @Test
    public void find(){
        QueryWrapper<Game> wrapper = new QueryWrapper<>();

        List<Game> games = gameMapper.selectList(wrapper);
        games.forEach(System.out::println);
    }

    @Test
    public void find2(){
        QueryWrapper<Game> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id").isNull("name");
        List<Game> games = gameMapper.selectList(wrapper);
        games.forEach(System.out::println);
    }

    @Test
    public void find3(){
        QueryWrapper<Game> wrapper = new QueryWrapper<>();
        wrapper.in("id",1,2);
        List<Game> games = gameMapper.selectList(wrapper);
        games.forEach(System.out::println);
    }

    @Test
    public void find4(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","张然");
        map.put("name","亮亮");
        List<Game> games = gameMapper.selectByMap(map);
        games.forEach(System.out::println);
    }

    @Test
    public void find5(){
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("name","张然");
//        map.put("name","亮亮");
        List<Map<String, Object>> games = gameMapper.selectMaps(null);
        games.forEach(System.out::println);
    }

    @Test
    public void find6(){
        QueryWrapper<Game> wrapper = new QueryWrapper<>();
        wrapper.eq("id",1);
        Game game = gameMapper.selectOne(wrapper);
        System.out.println("game = " + game);
    }

    @Test
    public void find7(){
        QueryWrapper<Game> wrapper = new QueryWrapper<>();
//        wrapper.eq("id",1);
        Page<Game> page = new Page<>(1,1);
//        page.setCurrent(2);
//        page.setSize(1);
        Page<Game> game = gameMapper.selectPage(page,wrapper);
        game.getRecords().forEach(System.out::println);

    }

    @Test
    public void find8(){
        List<Game> gameList = gameMapper.findall();
        gameList.forEach(System.out::println);

    }

    @Test
    public void find9(){
        List<Game> gameList = gameMapper.findall2();
        gameList.forEach(System.out::println);
    }

}
